package com.thread.test;

public class ThreeThread {

    private static volatile int gunSignal = 0;

    private static volatile int firstEndSignal = 0;

    private static   final int totalMeter= 100;

    public static void main(String[] args){

        final Thread gunThread = new Thread(new Runnable() {
            public void run() {
                try {
                    System.out.println("5秒后发令枪响!");
                    Thread.sleep(5000);
                    gunSignal = 1;
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        gunThread.start();
        new Thread(new Runnable() {
            public void run() {
                int start = 0;
                while(true){
                    if(gunSignal == 1  && firstEndSignal == 0){
                        System.out.println(Thread.currentThread().getName()+",启动,跑了:"+start);
                        try {
                            Thread.sleep(300);
                            start +=2;
                            if(start>totalMeter){
                                firstEndSignal = 1;
                                System.out.println(Thread.currentThread().getName()+",结束,总共跑了:"+start);
                                break;
                            }
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    if(firstEndSignal == 1){
                        System.out.println(Thread.currentThread().getName()+",结束,总共跑了:"+start);
                        break;
                    }

                }

            }
        },"T1").start();
        new Thread(new Runnable() {
            public void run() {
                int start = 0;
                while(true){
                    if(gunSignal == 1  && firstEndSignal == 0){
                        System.out.println(Thread.currentThread().getName()+",启动,跑了:"+start);
                        try {
                            Thread.sleep(300);
                            start +=3;
                            if(start>totalMeter){
                                firstEndSignal = 1;
                                System.out.println(Thread.currentThread().getName()+",结束,总共跑了:"+start);
                                break;
                            }
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    if(firstEndSignal == 1){
                        System.out.println(Thread.currentThread().getName()+",结束,总共跑了:"+start);
                        break;
                    }

                }

            }
        },"T2").start();
        new Thread(new Runnable() {
            public void run() {
                int start = 0;
                while(true){
                    if(gunSignal == 1 && firstEndSignal == 0){
                        System.out.println(Thread.currentThread().getName()+",启动,跑了:"+start);
                        try {
                            Thread.sleep(300);
                            start +=5;
                            if(start>=totalMeter){
                                firstEndSignal = 1;
                                System.out.println(Thread.currentThread().getName()+",结束,总共跑了:"+start);
                                break;
                            }
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    if(firstEndSignal == 1){
                        System.out.println(Thread.currentThread().getName()+",结束,总共跑了:"+start);
                        break;
                    }

                }

            }
        },"T3").start();

    }
}
